Array.prototype.contains = function(needle) {
	for (i in this) {
		if (this[i] == needle) return true;
	}
	return false;
}

/*
 * 数据存储操作storage
 */
function saveLocalData(keyStr, value) {
	if (!keyStr || !value) {
		return;
	}
	var newStr = JSON.stringify(value);
	plus.storage.setItem(keyStr, newStr);
}

function getLocalData(keyStr) {
	if (!keyStr) {
		return null;
	}
	var valueStr = plus.storage.getItem(keyStr);
	if (!valueStr) {
		return null;
	}
	var value = JSON.parse(valueStr);
	return value;
}

function getLocalIntData(keyStr) {
	var valueStr = plus.storage.getItem(keyStr);
	var value = JSON.parse(valueStr);
	return parseInt(value);
}


/*
 * 计算文件大小
 * 入参：字节大小
 * 出参：小数点后一位字符串，包含单位
 */
function getFileSize(fileByte) {
	var fileSizeByte = parseInt(fileByte);
	var fileSizeMsg = "";
	if (fileSizeByte < 1024) {
		fileSizeMsg = "1K";
	} else if (fileSizeByte < 1048576) {
		fileSizeMsg = parseInt(fileSizeByte / 1024) + "K";
	} else if (fileSizeByte == 1048576) {
		fileSizeMsg = "1M";
	} else if (fileSizeByte > 1048576 && fileSizeByte < 1073741824) {
		fileSizeMsg = (fileSizeByte / (1024 * 1024));

		if (fileSizeMsg % 1 != 0) {
			fileSizeMsg = fileSizeMsg.toFixed(1);
		}
		fileSizeMsg += "M";
	} else if (fileSizeByte > 1048576 && fileSizeByte == 1073741824) {
		fileSizeMsg = "1G";
	} else if (fileSizeByte > 1073741824 && fileSizeByte < 1099511627776) {
		fileSizeMsg = (fileSizeByte / (1024 * 1024 * 1024));
		if (fileSizeMsg % 1 != 0) {
			fileSizeMsg = fileSizeMsg.toFixed(1);
		}
		fileSizeMsg += "G";
	} else {
		fileSizeMsg = "1TB+";
	}
	return fileSizeMsg;
}

/*
 * 时间减1秒
 * 04:50 表示4分50秒
 */
function minuteSecondReduce(timeStr) {
	var timeArr = timeStr.split(':');
	var minute = parseInt(timeArr[0]);
	var second = parseInt(timeArr[1]);
	var descStr = '';
	if (second > 0) {
		second--;
		if (second < 10) {
			descStr = timeArr[0] + ":0" + second;
		} else {
			descStr = timeArr[0] + ":" + second;
		}
		return descStr;
	}
	minute--;
	second = 59;
	if (minute < 10) {
		descStr = '0' + minute + ":" + second;
	} else {
		descStr = minute + ":" + second;
	}
	return descStr;
}
/*
 * 时间减1秒
 * 00:04:50 表示4分50秒
 */
function hourMinuteSecondReduce(timeStr) {
	var timeArr = timeStr.split(':');
	var hour = parseInt(timeArr[0]);
	var minute = parseInt(timeArr[1]);
	var second = parseInt(timeArr[2]);
	var descStr = '';
	if (timeStr == '00:00:00') {
		return timeStr
	}
	if (second > 0) {
		second--;
	} else {
		minute--;
		second = 59;
		if (minute < 0) {
			minute = 0
			hour--
		}
	}
	return timeIntToTimeStr(hour, minute, second)
}


/*
 * 时间加1秒
 * 00:04:50 表示4分50秒
 */
function hourMinuteSecondPlus(timeStr) {
	var timeArr = timeStr.split(':');
	var hour = parseInt(timeArr[0]);
	var minute = parseInt(timeArr[1]);
	var second = parseInt(timeArr[2]);
	var descStr = '';
	if (second < 59) {
		second++;
	} else {
		minute++;
		second = 0;
		if (minute > 59) {
			minute = 0
			hour++
		}
	}
	return timeIntToTimeStr(hour, minute, second)
}

function timeToSecond(timeStr) {
	var timeArr = timeStr.split(':');
	var hour = parseInt(timeArr[0]);
	var minute = parseInt(timeArr[1]);
	var second = parseInt(timeArr[2]);
	return hour * 3600 + minute * 60 + second
}

function secondToTimeStr(sedonds) {
	var hour = parseInt(sedonds / 3600);
	var yu = sedonds % 3600;
	var minute = parseInt(yu / 60);
	var second = parseInt(yu % 60);
	return timeIntToTimeStr(hour, minute, second)
}

function timeIntToTimeStr(hour, minute, second) {
	var descStr = ''
	if (hour < 10) {
		descStr += '0'
	}
	descStr += hour + ":";
	if (minute < 10) {
		descStr += '0'
	}
	descStr += minute + ":";
	if (second < 10) {
		descStr += '0';
	}
	descStr += second;
	return descStr;
}

function minutesToHourMinutsSecond(minute) {
	var minute = parseInt(minute)
	var hour = parseInt(minute / 60)
	minute = minute % 60
	var str = ''
	if (hour < 10) {
		str += '0'
	}
	str += hour + ':'
	if (minute < 10) {
		str += '0'
	}
	str += minute + ':00'
	return str
}

/*
 * 存储本地文件信息
 */
function saveLocalFileInfo(ftpName, fileDic) {
	var localFileInfo = plus.storage.getItem('localFileInfo');
	var localFileDic = null;
	if (localFileInfo != null) {
		localFileDic = JSON.parse(localFileInfo);
	}
	if (localFileDic != null) {
		localFileDic[ftpName] = fileDic
	} else {
		localFileDic = {};
		localFileDic[ftpName] = fileDic
	}
	var localFileStr = JSON.stringify(localFileDic);
	plus.storage.setItem('localFileInfo', localFileStr);
}

/*
 * 读取本地文件信息
 */
function getLocalFileInfo(ftpName) {
	var localFileInfo = plus.storage.getItem('localFileInfo');
	var localFileDic = null;
	var oldLocalFileInfo = localStorage.getItem('localFileInfo');
	var oldLocalFileDic = null;

	if (localFileInfo != null) {
		localFileDic = JSON.parse(localFileInfo);
	}
	if (oldLocalFileInfo != null) {
		oldLocalFileDic = JSON.parse(oldLocalFileInfo);
	}
	//更换存储方法
	if (oldLocalFileDic != null) {
		for (var oneKey in oldLocalFileDic) {
			var oldLocalFileStr = oldLocalFileDic[oneKey];
			if (localFileDic == null) {
				localFileDic = {};
			}
			localFileDic[oneKey] = JSON.parse(oldLocalFileStr);
		}
		localStorage.removeItem('localFileInfo');
		var localFileStr = JSON.stringify(localFileDic);
		plus.storage.setItem('localFileInfo', localFileStr);
	}

	if (localFileDic == null) {
		return null;
	} else {
		var ftpDic = localFileDic[ftpName];
		if (ftpDic != null) {
			return ftpDic;
		} else {
			return null;
		}
	}
}


/*
 * 删除一个本地文件
 */
function deleteOneLocalFileInfo(ftpName) {
	var localFileInfo = plus.storage.getItem('localFileInfo');
	var localFileDic = null;
	if (localFileInfo != null) {
		localFileDic = JSON.parse(localFileInfo);
	}
	if (localFileDic != null) {
		delete localFileDic[ftpName];
		var localFileStr = JSON.stringify(localFileDic);
		plus.storage.setItem('localFileInfo', localFileStr);
	}
}

/*
 * 删除全部本地文件
 */
function deleteAllLocalFileInfo() {
	plus.storage.removeItem('localFileInfo');
}

/*
 * 存储消息时间
 */
function saveMessageTime(messageTime, keyStr, usercode) {
	var valueStr = plus.storage.getItem('newMsgDic');
	var oldMsg = null;
	if (valueStr != null) {
		oldMsg = JSON.parse(valueStr);
	}
	if (oldMsg) {
		var uidDic = oldMsg[usercode];
		if (!uidDic) {
			uidDic = {};
		}
		uidDic[keyStr] = messageTime;
		oldMsg[usercode] = uidDic;
	} else {
		oldMsg = {};
		var uidDic = {};
		uidDic[keyStr] = messageTime;
		oldMsg[usercode] = uidDic
	}
	var newStr = JSON.stringify(oldMsg);
	plus.storage.setItem('newMsgDic', newStr);
}

/*
 * 读取消息时间
 * 存储格式 newMsgDic = {'04480':{'hlxw':'','hlbfb':''},
 * 						'04430':{'hlxw':'','hlbfb':''},...}
 */
function getMessageTime(keyStr, usercode) {
	//清除原来的
	var messageDic = plus.storage.getItem('messageDic');
	if (messageDic) {
		plus.storage.removeItem('messageDic');
	}
	if (!keyStr || keyStr == '') {
		return '';
	}
	var valueStr = plus.storage.getItem('newMsgDic');
	var oldMsg = null;
	if (valueStr != null) {
		oldMsg = JSON.parse(valueStr);
		var uidDic = oldMsg[usercode];
		if (uidDic) {
			var messageTime = uidDic[keyStr]||'';
			return messageTime;
		} else {
			return '';
		}
	} else {
		return '';
	}
}


/*
 * 压疮 获取时间
 * 日 大于等于7时 开始时间是本月1日 结束时间是当日
 * 日 小于7时     开始时间是上个月1日 结束时间是上个月月末
 */
function getStartAndEndTime() {
	var thisDate = new Date();
	var currentYear = thisDate.getFullYear();
	var lastMonth = thisDate.getMonth();
	var currentMonth = thisDate.getMonth() + 1;
	var currentDate = thisDate.getDate();
	var startTime, endTime;
	if (currentDate >= 7) {
		var cMonthInt = parseInt(currentMonth, 10);
		var cDateInt = parseInt(currentDate, 10)
		var cMonthStr = currentMonth;
		var cDateStr = currentDate;
		if (cMonthInt < 10) {
			cMonthStr = "0" + cMonthInt;
		}
		if (cDateInt < 10) {
			cDateStr = "0" + cDateInt;
		}
		startTime = currentYear + '-' + cMonthStr + '-' + '01';
		endTime = currentYear + '-' + cMonthStr + '-' + cDateStr;
	} else {

		var month = parseInt(currentMonth, 10) + 1;
		var temp = new Date(currentYear + "/" + month + "/0");
		var lastDays = temp.getDate();
		if (lastMonth == 0) {
			var lastYear = parseInt(currentYear, 10) - 1;
			startTime = lastYear + '-12-01';
			endTime = lastYear + '-12-31';
		} else {
			var lMonthInt = parseInt(lastMonth, 10);
			var lMonthStr = lastMonth;
			if (lMonthInt < 10) {
				lMonthStr = "0" + lMonthInt;
			}
			var monthArray = [4, 6, 9, 11]
			if (lMonthInt == 2) {
				lastDays = '28';
				var cYearInt = parseInt(currentYear, 10);
				if (cYearInt % 400 == 0 || (cYearInt % 100 != 0 && cYearInt % 4 == 0)) { //闰年
					lastDays = '29';
				}

			} else if (monthArray.contains(lMonthInt)) {
				lastDays = '30'
			} else {
				lastDays = '31';
			}
			startTime = currentYear + '-' + lMonthStr + '-' + '01';
			endTime = currentYear + '-' + lMonthStr + '-' + lastDays;
		}
	}
	return [startTime, endTime];
}

function getYearArray(startTime, endTime) {
	var stYearStr = startTime.substr(0, 4);
	var endYearStr = endTime.substr(0, 4);
	var stYearInt = parseInt(stYearStr);
	var endYearInt = parseInt(endYearStr);
	var yearArray = [];
	for (var i = 0; i <= endYearInt - stYearInt; i++) {
		yearArray[i] = stYearInt + i;
	}
	return yearArray;
}

/*
 * OC中保存 js中获取
 * OC 中正常保存就行
 */
function getUserDefault(key) {
	if (key == null) {
		return null;
	}
	var UserDefaultsClass = plus.ios.importClass('NSUserDefaults');
	var standardUserDefaults = UserDefaultsClass.standardUserDefaults();
	return plus.ios.invoke(standardUserDefaults, "objectForKey:", key);
}

/*
 * js中保存 OC中获取
 * OC中正常获取就行
 */
function setUserDefault(key, value) {
	if (value == null || key == null) {
		return;
	}
	var UserDefaultsClass = plus.ios.importClass("NSUserDefaults");
	var standardUserDefaults = UserDefaultsClass.standardUserDefaults();
	plus.ios.invoke(standardUserDefaults, "setObject:forKey:", value, key);
	plus.ios.invoke(standardUserDefaults, "synchronize");
}


function getElementAbsPosition(item) {
	var actualLeft = item.offsetLeft;
	var actualTop = item.offsetTop;
	var current = item.offsetParent;
	while (current) {
		actualLeft += (current.offsetLeft + current.clientLeft);
		actualTop += (current.offsetTop + current.clientTop);
		current = current.offsetParent;
	}
	return {
		'left': actualLeft,
		'top': actualTop
	};
}
//处理特殊字符
function dealSepcialCharacter(str) {
	var valueStr = str;
	valueStr = valueStr.replace(/ /g, '&nbsp;');
	valueStr = valueStr.replace(/@and@/g, '&');
	valueStr = valueStr.replace(/@《@/g, '<');
	valueStr = valueStr.replace(/@》@/g, '>');
	valueStr = valueStr.replace(/@jianhao@/g, '^');
	valueStr = valueStr.replace(/@br@/g, '<br />');
	return valueStr;
}

function changeSepcialStr(str) {
	var valueStr = str;
	valueStr = valueStr.replace(/\n/g, '@br@');
	valueStr = valueStr.replace(/ /g, '&nbsp;');
	valueStr = valueStr.replace(/&/g, '@and@');
	valueStr = valueStr.replace(/</g, '@《@');
	valueStr = valueStr.replace(/>/g, '@》@');
	valueStr = valueStr.replace(/^/g, '@jianhao@');
	return valueStr;
}

function dealParamsArr(arr) {
	var destStr = '';
	for (var i = 0; i < arr.length; i++) {
		var str = arr[i] + '';
		if (str != '') {
			str = dealParamsStr(str)
		}
		if (i == arr.length - 1) {
			destStr += str;
		} else {
			destStr += str + '^';
		}
	}
	return destStr;
}

function dealParamsStr(str) {
	var valueStr = str;
	valueStr = valueStr.replace(/</g, '@《@');
	valueStr = valueStr.replace(/>/g, '@》@');
	valueStr = valueStr.replace(/^/g, '@jianhao@');
	valueStr = valueStr.replace(/&/g, '@and@');
	valueStr = valueStr.replace(/|/g, '@shuxian@');
	return valueStr;
}

function dealDecryptStr(str) {
	var dataStr = str;
	dataStr = dataStr.replace(/\n/g, '@br@');
	dataStr = dataStr.replace(/&/g, '@and@');
	return dataStr;
}

function dealSuccessBlockStr(str) {
	var valueStr = str;
	valueStr = valueStr.replace(/ /g, '&nbsp;');
	valueStr = valueStr.replace(/@and@/g, '&');
	valueStr = valueStr.replace(/@《@/g, '<');
	valueStr = valueStr.replace(/@》@/g, '>');
	valueStr = valueStr.replace(/@jianhao@/g, '^');
	valueStr = valueStr.replace(/@shuxian@/g, '|');
	valueStr = valueStr.replace(/@br@/g, '<br />');
	return valueStr;
}

function jsonSort(array, field, reverse) {
	//数组长度小于2 或 没有指定排序字段 或 不是json格式数据
	if (array.length < 2 || !field || typeof array[0] !== "object") return array;
	//数字类型排序
	if (typeof array[0][field] === "number") {
		array.sort(function(x, y) {
			return x[field] - y[field]
		});
	}
	//字符串类型排序
	if (typeof array[0][field] === "string") {
		array.sort(function(x, y) {
			return x[field].localeCompare(y[field])
		});
	}
	//倒序
	if (reverse) {
		array.reverse();
	}
	return array;
}

function getFirstDayInWholeMonth(today) {
	ymDict = getYMDWithStr(today)
	var year = ymDict.year;
	var month = ymDict.month
	currYM = outYMStr(year, month);
	var maxDay = getMonthMaxDay(year, month);
	var lastMaxDay = getLastMonthMaxDay(year, month);
	var ymStr = outYMStr(year, month)
	var startIndex = getWeekIndex(ymStr + '-01'); //1到7
	var endIndex = getWeekIndex(ymStr + '-' + maxDay);
	if (startIndex == 0) {
		startIndex = 7
	}
	if (endIndex == 0) {
		endIndex = 7
	}
	var count = startIndex + maxDay + (6 - endIndex);
	for (var i = 0; i < count; i++) {
		if (i < startIndex - 1) { //上个月的
			var dayStr = lastMaxDay - (startIndex - 1) + i + 1;
			var lmonth = month - 1;
			var lyear = year;
			if (lmonth == 0) {
				lmonth = 12;
				lyear -= 1;
			}
			var idStr = '' + lyear + '-' + lmonth + '-' + dayStr;
			if (lmonth < 10) {
				idStr = '' + lyear + '-0' + lmonth + '-' + dayStr;
			}
			if (i == 0) {
				return idStr
			}
		} else if (i >= (startIndex - 1) + maxDay) { //下个月
			var nmonth = month + 1;
			var nyear = year;
			if (nmonth == 13) {
				nmonth = 1;
				nyear += 1;
			}
			var dayStr = (i - maxDay - (startIndex - 1) + 1);
			var idStr = '' + nyear + '-' + nmonth + '-0' + dayStr;
			if (nmonth < 10) {
				idStr = '' + nyear + '-0' + nmonth + '-0' + dayStr;
			}
			if (i == count - 1) {
				endDate = idStr
			}

		} else {
			var monthStr = month + '';
			var dayStr = (i - (startIndex - 1) + 1);
			if (month < 10) {
				monthStr = '0' + month;
			}
			var idStr = '' + year + '-' + monthStr + '-' + dayStr;
			if ((i - (startIndex - 1) + 1) < 10) {
				idStr = '' + year + '-' + monthStr + '-0' + dayStr;
			}
			if (i == 0) {
				startDate = idStr
			} else if (i == count - 1) {
				endDate = idStr
			}
			if (i == 0) {
				return (idStr)
			}
		}
	}
}

function fileTypeIcon(url) {
	var icon = ''
	url = url.toLowerCase()
	var urlArr = url.split('.')
	if (url.indexOf('.') != -1) {
		url = urlArr[urlArr.length -1] 
	}
	if (url == 'pdf') {
		icon = 'pdf'
	} else if (url == "png" || url == "jpg" || url == "jpeg" || url == "gif" || url == "bmp") {
		icon = 'img'
	} else if (url == "doc" || url == "docx") {
		icon = 'word'
	} else if (url == "ppt" || url == "pptx") {
		icon = 'ppt'
	} else if (url == "txt" || url == "json") {
		icon = 'txt'
	} else {
		icon = 'file'
	}
	return icon
}
function dealUndefined(str){
	if (str == undefined || str == 'undefined' || str == null || str == 'null') {
		str = ''
	}
	return str
}